package com.cq.xjms.web.actions.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cq.xjms.model.Clazz;
import com.cq.xjms.model.Department;
import com.cq.xjms.model.Institute;
import com.cq.xjms.model.Student;
import com.cq.xjms.model.Teacher;
import com.cq.xjms.model.User;
import com.cq.xjms.service.SysManager;
import com.cq.xjms.util.Constants;
import com.cq.xjms.web.actions.BaseAction;

public class SysAction extends BaseAction{
	private static Logger log = Logger.getLogger(SysAction.class);
	private static final long serialVersionUID = -879633312645153566L;
	private String type;
	private Clazz clazz;
	private SysManager sysManager;
	private User user;
	private List<User> users;
	private List<Clazz> clazzs;
	private Institute institute;
	private List<Institute> institutes;
	private Department department;
	private List<Department> departments;
	private int loginType;
	private Teacher teacher;
	private Map<String,Object> params = new HashMap<String,Object>();

	//用户登录
	public String login(){
		Object obj = sysManager.login(user.getUserNo(), user.getPassword(),loginType);
		if(obj == null){
			List list = new ArrayList();
			list.add("账号或密码错误!");
			this.setActionErrors(list);
			return INPUT;
		}
	    getSession().setAttribute(Constants.LOGIN_USER, obj);
		return SUCCESS;
	}
	//退出系统�
	public String logout(){
		getSession().invalidate();
		return SUCCESS;
	}
	public String addUser(){
		return INPUT;
	}
	public String edit(){
		user = (User)sysManager.load(User.class, user.getId());
		return SUCCESS;
	}
	public String editSave(){
		try{
			log.info(user.getId());
			String password = ((User)sysManager.load(User.class, user.getId())).getPassword();
			user.setPassword(password);
			sysManager.saveOrUpdate(user);
		
		}catch(Exception e){
			log.error("update user failed !",e);
			return INPUT;
		}
		return SUCCESS;
		
	}
	//保存用户
	public String saveUser(){
		try{
			params.put("userNo", user.getUserNo());
			users = sysManager.find(User.USER_QUERY,params);
			if(users==null || users.size()==0){
				sysManager.save(user);
			}else{
//					params.clear();
//					params.put("user.userNo", "璇ョ敤鎴风紪鍙峰凡瀛樺湪锛�");
//					this.setFieldErrors(params);
				List list = new ArrayList();
				list.add("此用户编号已经存在！");
				this.setActionErrors(list);
				return INPUT;
			}	
			type = "add";
			
			
		}catch(Exception e){
			log.error("save user failed",e);
			return INPUT;
		}
		user = null;
		return SUCCESS;
	}
	
	public String deleteUser(){
		sysManager.remove(User.class, user.getId());
		return SUCCESS;
	}
	//查询用户
	public String queryUser(){
		LOG.info(user);
		try{
		if(user!=null){
			if(StringUtils.isNotEmpty(user.getName()))
				params.put("name", user.getName());
			if(StringUtils.isNotEmpty(user.getUserNo()))
				params.put("userNo", user.getUserNo());
			if(StringUtils.isNotEmpty(user.getEmail()))
				params.put("email", user.getEmail());
		}
		totalRows = sysManager.countUsers(params);
		users = sysManager.queryUsers(params,getLimit());
		}catch(Exception e){
			LOG.error("query user failed", e);
		}
		return SUCCESS;
	}
	
	
	public String addInstitute(){
		
		return SUCCESS;
	}
	
	public String saveInstitute(){
		
		try{
			params.put("code",institute.getCode());
			institutes = sysManager.find(Institute.INSTITUTE_QUERY,params);
			if(institutes==null || institutes.size()==0){
				sysManager.save(institute);
			}else{
//					params.clear();
//					params.put("user.userNo", "璇ョ敤鎴风紪鍙峰凡瀛樺湪锛�");
//					this.setFieldErrors(params);
				List list = new ArrayList();
				list.add("此学院编号已经存在！");
				this.setActionErrors(list);
				return INPUT;
			}	
			type = "add";
			
			
		}catch(Exception e){
			log.error("save user failed",e);
			return INPUT;
		}
		department = null;
		return SUCCESS;
	}
	public String queryById(){
		try{
			Object obj = getRequest().getSession().getAttribute(Constants.LOGIN_USER);
			if(obj!=null){
				if(obj instanceof User){
					user = (User)obj;
					return "user";
				}else if (obj instanceof Teacher){
					teacher = (Teacher)obj;
					return "teacher";
				}
			}
		}catch(Exception e){
			log.error("",e);
		}
		return NONE;
	}
	
	public String listInstitute(){
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	public String editInstitute(){
		institute = (Institute)sysManager.load(Institute.class, institute.getId());
		return SUCCESS;
	}
	public String editInstituteSave(){
		
		try{
			log.info(institute.getId());
			sysManager.saveOrUpdate(institute);
		
		}catch(Exception e){
			log.error("update institute failed !",e);
			return INPUT;
		}
		return SUCCESS;
	}
	public String deleteInstitute(){
		params.put("id", institute.getId());
		departments=sysManager.find(Department.DEPARTMENT_QUERYById, params);
		if(departments !=null&&departments.size()>0){
			institutes = sysManager.find(Institute.INSTITUTE_LIST);
			type="del";
			institute = null;
			return INPUT;
		}else{
			sysManager.remove(Institute.class, institute.getId());
		}
		
		
		return SUCCESS;
	}
	
	

	public String addDepartment(){
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	public String saveDepartment(){
			
			try{
				params.put("code",department.getCode());
				departments = sysManager.find(Department.DEPARTMENT_QUERY,params);
				if(departments==null || departments.size()==0){
					sysManager.save(department);
					type = "add";
				}else{
	//					params.clear();
	//					params.put("user.userNo", "璇ョ敤鎴风紪鍙峰凡瀛樺湪锛�");
	//					this.setFieldErrors(params);
					List list = new ArrayList();
					list.add("此系编号已经存在！");
					this.setActionErrors(list);
					
				}	
				
				
				
			}catch(Exception e){
				log.error("save user failed",e);
				return INPUT;
			}
			department = null;
			institutes = sysManager.find(Institute.INSTITUTE_LIST);
			return SUCCESS;
		}
		
	public String listDepartment(){
		departments = sysManager.find(Department.DEPARTMENT_LIST);
		return SUCCESS;
	}
	public String listInstitueJson(){
		Map<String,Object> params = null;
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		JSONArray arr = new JSONArray();
		try{
			for(Institute d : institutes){
				JSONObject obj = new JSONObject();
				obj.put("id", d.getId());
				obj.put("name", d.getName());
				arr.add(obj);
			}
		}catch(Exception e){
			log.error("",e);
		}
		this.writeJSON(arr.toString());
		return NONE;
	}
	public String listDepartmentJson(){
		Map<String,Object> params = null;
		if(institute!=null){
			params = new HashMap<String,Object>();
			params.put("id", institute.getId());
		}
		departments = sysManager.find(Department.DEPARTMENT_QUERYById,params);
		JSONArray arr = new JSONArray();
		try{
			for(Department d : departments){
				JSONObject obj = new JSONObject();
				obj.put("id", d.getId());
				obj.put("name", d.getName());
				arr.add(obj);
			}
		}catch(Exception e){
			log.error("",e);
		}
		
	
		this.writeJSON(arr.toString());
		return NONE;
	}
	
	public String loadDepartmentJson(){
		Map<String,Object> params = null;
		if(institute!=null){
			params = new HashMap<String,Object>();
			params.put("id", institute.getId());
		}
		departments = sysManager.find(Department.DEPARTMENT_LIST,params);
		JSONArray arr = new JSONArray();
		try{
			for(Department d : departments){
				JSONObject obj = new JSONObject();
				obj.put("id", d.getId());
				obj.put("name", d.getName());
				arr.add(obj);
			}
		}catch(Exception e){
			log.error("",e);
		}
		
	
		this.writeJSON(arr.toString());
		return NONE;
	}
	
	
	public String editDepartment(){
		department = (Department)sysManager.load(Department.class, department.getId());
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	public String editDepartmentSave(){
			
			try{
				log.info(department.getId());
				sysManager.saveOrUpdate(department);
			
			}catch(Exception e){
				log.error("update department failed !",e);
				return INPUT;
			}
			
			return SUCCESS;
		}
	public String deleteDepartment(){
		params.put("id", department.getId());
		clazzs=sysManager.find(Clazz.CLAZZ_LIST, params);
		if(clazzs !=null&&clazzs.size()>0){
			departments = sysManager.find(Department.DEPARTMENT_LIST);
			type="del";
			department = null;
			return INPUT;
		}
		sysManager.remove(Department.class, department.getId());
		return SUCCESS;
	}
	
	
	
	public String getType() {
		return type;
	}
	
	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}

	public void setType(String type) {
		this.type = type;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	public Institute getInstitute() {
		return institute;
	}
	public void setInstitute(Institute institute) {
		this.institute = institute;
	}
	public List<Institute> getInstitutes() {
		return institutes;
	}
	public void setInstitutes(List<Institute> institutes) {
		this.institutes = institutes;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	public List<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	public Clazz getClazz() {
		return clazz;
	}
	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}
	public Map<String, Object> getParams() {
		return params;
	}
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	public SysManager getSysManager() {
		return sysManager;
	}
	public List<Clazz> getClazzs() {
		return clazzs;
	}
	public void setClazzs(List<Clazz> clazzs) {
		this.clazzs = clazzs;
	}
	public int getLoginType() {
		return loginType;
	}
	public void setLoginType(int loginType) {
		this.loginType = loginType;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	
	
}
